package com.ey12.db.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmAccess;

/**
 * A data access object (DAO) providing persistence and search support for
 * GmAccess entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *    
 * @see com.ey12.db.domain.GmAccess
 * @author MyEclipse Persistence Tools
 */

public class GmAccessDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmAccessDAO.class);

	// property constants
	public static void main(String[] a){
		System.out.println("111");
	}
	

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmAccess transientInstance) {
		log.debug("saving GmAccess instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmAccess persistentInstance) {
		log.debug("deleting GmAccess instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public List findAccessByName(String username){
		try {
			String queryString = " from GmAccess g where g.gmUser.userName='"+username+"'";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public List findAllAccess() {
		log.debug("finding all GmAccess instances");
		try {
			String queryString = "from GmAccess g where g.isChecked=1";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	
	public List findSellViewFirstCbx(String username) {
		try {
			System.out.println("findSellViewFirstCbx() used");
			String queryString = "from GmAccess g where g.gmUser.userName=? and g.isChecked=2 order by g.gmGame.gameName";
			System.out.println("findSellViewFirstCbx()="+queryString);
			return getHibernateTemplate().find(queryString,username);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	public List findSellViewSecondCbx(String username,String gameName) {
		try {
			String queryString = " from GmAccess g where g.gmUser.userName='"+username+"' and g.gmGame.gameName='"+gameName+"' and g.isChecked=2 order by g.gmGame.gameName";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	

	public GmAccess findById(java.lang.Integer id) {
		log.debug("getting GmAccess instance with id: " + id);
		try {
			GmAccess instance = (GmAccess) getHibernateTemplate().get(
					"com.ey12.db.domain.GmAccess", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmAccess instance) {
		log.debug("finding GmAccess instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmAccess instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmAccess as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all GmAccess instances");
		try {
			String queryString = "from GmAccess";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public GmAccess merge(GmAccess detachedInstance) {
		log.debug("merging GmAccess instance");
		try {
			GmAccess result = (GmAccess) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmAccess instance) {
		log.debug("attaching dirty GmAccess instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmAccess instance) {
		log.debug("attaching clean GmAccess instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmAccessDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmAccessDAO) ctx.getBean("GmAccessDAO");
	}
}